<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 钟房铃声控制器
 * @package app\index\controller
 */
class RoomBell extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取房间类型
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $getData = $this->request->post();
            $sql = Db::name('qt_room_bell')
                ->alias('b')
                ->leftJoin('qt_room r','r.id = b.room_id')
                ->field('b.id,r.name,b.mac')
                ->where(['r.store_id' => $userInfo['store_id']]);
            if(isset($getData['name']) && !empty($getData['name'])){
                $sql = Db::name('qt_room_bell')
                    ->alias('b')
                    ->leftJoin('qt_room r','r.id = b.room_id')
                    ->field('b.id,r.name,b.mac')
                    ->where(['r.store_id' => $userInfo['store_id']])->whereLike('r.name','%'.$getData['name'].'%');
            }
            $data = $sql->order('b.id desc')->paginate($getData['limit']);
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加房间
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['mac']) || empty($getData['room_id'])){
                $this->result('缺少参数', '', 204);
            }
            $insert = Db::name('qt_room_bell')->insert($getData);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑保存房间
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['mac']) || empty($getData['room_id']) || empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $update= Db::name('qt_room_bell')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑保存房间
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_room_bell')->where('id',$getData['id'])->find();
            $this->result('获取成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除钟铃
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_room_bell')->where('id',$getData['id'])->delete();
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }



}
